package com.sctu.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * Created by Mr-zhu on 2015/9/25.
 */
public class SharePreferenceUtil {

    private  SharedPreferences sharedPreferences;
    private  SharedPreferences.Editor editor;
    private static SharePreferenceUtil single = null;

    //静态工厂方法
    public static SharePreferenceUtil getInstance(Context context) {
        if(single == null){
            single = new SharePreferenceUtil(context);
        }
        return single;
    }

    public SharePreferenceUtil(Context context){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        editor = sharedPreferences.edit();//获得Editor
    }


    public void setKeyData(String key,Object value){
//        if(value instanceof String){
//            editor.putString(key,value.toString());}  //判段类型存值
        editor.putString(key,value.toString());
       //存入值
        editor.commit();//编辑完成后提交
    }

    public String getKeyData(String key){

        String value = sharedPreferences.getString(key, "");//第二个参数为默认值,找不到则返回""
        return value;
    }
}
